package com.example.ai.uploadImage;


import com.jcraft.jsch.*;

import java.io.InputStream;

public class RemoteScriptExecutor {
    public static void main(String[] args) {
        String host = "10.2.188.107";
        int port = 22;
        String username = "root";
        String password = "zjzhjs-2023"; // 建议使用密钥认证
        String pythonPath = "/root/yolo11/bin/python"; // Python解释器路径
        String scriptPath = "/root/yolo-project/fastdemo.py"; // Python脚本路径
        String arguments = "arg1 arg2"; // 可选参数
        String workingDir = "/root/yolo-project";

        String activate = "source /root/yolo11/bin/activate";
        String yoloDir = "/root/yolo-project"; // YOLO项目目录
        String dataYaml = "data.yaml"; // 数据集配置
        String model = "yolov11n.pt"; // 预训练模型
        int epochs = 10;
        int batchSize = 16;

        try {
            String command = String.format(
                    "cd %s && %s && yolo detect train data=data.yaml model=yolov11n.pt epochs=100 imgsz=640",
                    yoloDir, activate
            );
//            String command = String.format("cd %s && %s %s %s",
//                    workingDir, pythonPath, scriptPath, arguments);
            executeRemoteCommand(host, port, username, password,
                    command);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void executeRemoteCommand(String host, int port, String username,
                                            String password, String command) throws JSchException {
        JSch jsch = new JSch();
        Session session = null;
        Channel channel = null;

        try {
            // 创建SSH会话
            session = jsch.getSession(username, host, port);
            session.setPassword(password);

            // 关闭主机密钥检查（生产环境应配置已知主机）
            java.util.Properties config = new java.util.Properties();
            config.put("StrictHostKeyChecking", "no");
            session.setConfig(config);

            session.connect();

            // 执行命令
            channel = session.openChannel("exec");
            ((ChannelExec) channel).setCommand(command);

            // 获取输入流（命令输出）
            channel.setInputStream(null);
            ((ChannelExec) channel).setErrStream(System.err);

            InputStream in = channel.getInputStream();
            channel.connect();

            // 读取命令输出
            byte[] tmp = new byte[1024];
            while (true) {
                while (in.available() > 0) {
                    int i = in.read(tmp, 0, 1024);
                    if (i < 0) break;
                    System.out.print(new String(tmp, 0, i));
                }
                if (channel.isClosed()) {
                    if (in.available() > 0) continue;
                    System.out.println("Exit status: " + channel.getExitStatus());
                    break;
                }
                Thread.sleep(1000);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 关闭连接
            if (channel != null) channel.disconnect();
            if (session != null) session.disconnect();
        }
    }
}
